<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue.js"></script>
    <script src="./lib/axios.js"></script>
</head>

<body>
    <div id="app">
        <input type="button" value="get请求" @click="requestGet()">
        <input type="button" value="post请求" @click="requestPost()">
    </div>
</body>
<script>
    // 配置全局根路径
    axios.defaults.baseURL = 'http://localhost:8080';

    var vm = new Vue({
        el: "#app",
        data: {
            // 这里会有跨域的问题，因为我没有搭服务器。现在我搭了。axios配置全局根路径后不需要像vue-resource一样写相对路径。
            getUrl: '/demo/requestGet',
            postUrl: '/demo/requestPost'
        },
        methods: {
            requestGet() {
                axios.get(this.getUrl, {
                    emulateJSON: true
                }).then(response => {
                    console.log(response);
                }).catch(error => {
                    console.log(error);
                });
            },
            requestPost() {
                axios.post(this.postUrl, {
                    emulateJSON: true
                }).then(response => {
                    console.log(response);
                }).catch(error => {
                    console.log(error);
                });
            }
            
        }
    });
</script>

</html>